//
//  KRChoiceVC.m
//  QuickCare
//
//  Created by 张凯锐 on 16/7/13.
//  Copyright © 2016年 lidabiao. All rights reserved.
//

#import "KRChoiceVC.h"
#import "KRChoiceCell.h"
#import "KRChoice.h"

@interface KRChoiceVC ()<UITableViewDelegate,UITableViewDataSource>
//文本输入框
@property (weak, nonatomic) IBOutlet UITextField *choiceTextField;
//列表数组
@property (nonatomic,strong) NSMutableArray *choiceArr;
//搜索数组
@property (nonatomic,strong) NSMutableArray *searchArr;
//友好提示
@property (nonatomic,copy) NSString *prompt;

//加载数据索引
@property (nonatomic,assign) NSInteger page;
@end

@implementation KRChoiceVC
- (void)viewDidLoad {
    [super viewDidLoad];
    [SVProgressHUD showWithStatus:@"加载中..."];
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(requestData)];
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadData)];
    [self.tableView.mj_header beginRefreshing];
    
    //上拉时导航条会消失，下拉出现
    [self.navigationController setHidesBarsOnSwipe:YES];
}

//请求数据
- (void)requestData {
    [self.choiceArr removeAllObjects];
    self.page = 1;
    //url
    NSString *url = @"http://iosapi.itcast.cn/doctor/searchCI3List.json.php";
    //请求参数
    NSDictionary *parameters = @{@"page":@(self.page),@"page_size":@15,@"ci1_id":@(self.index+1),@"keyword":@""};
    [[NetworkManager shareManager] requestDataWithURLString:url parameters:parameters success:^(NSDictionary * response) {
        [self.tableView.mj_header endRefreshing];
        //用YYModel字典转模型
        NSArray *data = [NSArray yy_modelArrayWithClass:[KRChoice class] json:response[@"data"]];
        [self.choiceArr addObjectsFromArray:data];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [SVProgressHUD dismiss];
            //取完数据刷新tableView
            [self.tableView reloadData];
        });
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
    }];
}

//加载数据
- (void)loadData {
    self.page += 1;
    //url
    NSString *url = @"http://iosapi.itcast.cn/doctor/searchCI3List.json.php";
    //请求参数
    NSDictionary *parameters = @{@"page":@(self.page),@"page_size":@15,@"ci1_id":@(self.index+1),@"keyword":@""};
    [[NetworkManager shareManager] requestDataWithURLString:url parameters:parameters success:^(NSDictionary * response) {
        
        [self.tableView.mj_footer endRefreshing];
        //用YYModel字典转模型
        NSArray *data = [NSArray yy_modelArrayWithClass:[KRChoice class] json:response[@"data"]];
        [self.choiceArr addObjectsFromArray:data];
        //取完数据刷新tableView
        [self.tableView reloadData];
        
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
    }];
}

#pragma mark - 搜索方法
- (IBAction)clickSearch:(id)sender {
    //将上次的数据清空
    self.prompt = @"";
    [self.searchArr removeAllObjects];
    //遍历数据
    for (int i = 0; i < self.choiceArr.count; i++) {
        KRChoice *choice = self.choiceArr[i];
        //使用谓词搜索
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self CONTAINS[cd] %@",self.choiceTextField.text];
        //如果有则为YES
        BOOL result = [predicate evaluateWithObject:choice.ci3_name];
        //有就添加到数组中
        if (result) {
            [self.searchArr addObject:self.choiceArr[i]];
        } else {
            //搜索框有文字但没有想要的选择才提示
            if (![self.choiceTextField.text isEqualToString:@""]) {
                self.prompt = @"没有您想要的选择";
            }
        }
    }
    [self.tableView reloadData];
}

#pragma mark - UITableViewDataSource数据源
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (self.searchArr.count) {
        return self.searchArr.count;
    } else if ([self.prompt isEqualToString:@"没有您想要的选择"]) {
        return 1;
    } else {
        return self.choiceArr.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    KRChoiceCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (self.searchArr.count) {
        cell.searchText = self.choiceTextField.text;
        cell.result = YES;
        cell.choice = self.searchArr[indexPath.row];
    } else if ([self.prompt isEqualToString:@"没有您想要的选择"]) {
        cell.textLabel.text = @"没有您想要的选择";
        cell.accessoryType = UITableViewCellAccessoryNone;
    }else {
        cell.choice = self.choiceArr[indexPath.row];
    }
    return cell;
}

#pragma mark - UITableViewDelegate代理
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    KRChoiceCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if ([self.delegate respondsToSelector:@selector(choiceVCDelegate:choiceName:ci2_id:)]) {
        [self.delegate choiceVCDelegate:self choiceName:cell.textLabel.text ci2_id:cell.choice.ci2_id];
    }
    [self.navigationController popViewControllerAnimated:YES];
}

//懒加载
- (NSMutableArray *)choiceArr {
    if (_choiceArr == nil) {
        _choiceArr = [NSMutableArray array];
    }
    return _choiceArr;
}

- (NSMutableArray *)searchArr {
    if (_searchArr == nil) {
        _searchArr = [NSMutableArray array];
    }
    return _searchArr;
}


@end
